<template>
  <div class="app-container">
    <el-form :model="queryLldParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="100px">
      <el-form-item label="领料单位名称" prop="lldwName">
        <el-input
          v-model="queryLldParams.lldwName"
          placeholder="请输入领料单位名称"
          clearable
          @keyup.enter.native="handleLldQuery"
        />
      </el-form-item>
      <el-form-item label="领用时间">
        <el-date-picker
          v-model="daterangeLyTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="课题名称" prop="ktName">
        <el-input
          v-model="queryLldParams.ktName"
          placeholder="请输入课题名称"
          clearable
          @keyup.enter.native="handleLldQuery"
        />
      </el-form-item>
      <el-form-item label="课题负责人" prop="ktFzr">
        <el-input
          v-model="queryLldParams.ktFzr"
          placeholder="请输入课题负责人"
          clearable
          @keyup.enter.native="handleLldQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="物资主管" prop="wzzg">-->
      <!--        <el-input-->
      <!--          v-model="queryLldParams.wzzg"-->
      <!--          placeholder="请输入物资主管"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleLldQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="发料人" prop="flr">-->
      <!--        <el-input-->
      <!--          v-model="queryLldParams.flr"-->
      <!--          placeholder="请输入发料人"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleLldQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="领料人" prop="llr">-->
      <!--        <el-input-->
      <!--          v-model="queryLldParams.llr"-->
      <!--          placeholder="请输入领料人"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleLldQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleLldQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetLldQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleLldAdd"
          v-hasPermi="['fyhs:lld:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleLldUpdate"
          v-hasPermi="['fyhs:lld:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleLldDelete"
          v-hasPermi="['fyhs:lld:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleLldExport"
          v-hasPermi="['fyhs:lld:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getLldList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="lldList" @selection-change="handleSelectionLldChange"
              border :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="领料单ID" align="center" prop="lldId"/>
      <el-table-column label="课题ID" align="center" prop="ktId"/>
      <el-table-column label="领料单位名称" align="center" prop="lldwName"/>
      <el-table-column label="领用时间" align="center" prop="lyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课题名称" align="center" prop="ktName"/>
      <el-table-column label="课题负责人" align="center" prop="ktFzr"/>
      <el-table-column label="物资主管" align="center" prop="wzzg"/>
      <el-table-column label="发料人" align="center" prop="flr"/>
      <el-table-column label="领料人" align="center" prop="llr"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleLldUpdate(scope.row)"
            v-hasPermi="['fyhs:lld:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleLldDelete(scope.row)"
            v-hasPermi="['fyhs:lld:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="LldTotal>0"
      :total="LldTotal"
      :page.sync="queryLldParams.pageNum"
      :limit.sync="queryLldParams.pageSize"
      @pagination="getLldList"
    />

    <!-- 添加或修改费用核算领料单对话框 -->
    <el-dialog :title="title" :visible.sync="openAddKtLld" width="70%" append-to-body>

      <el-form ref="formDialog" :model="LldForm" :rules="rulesLld" label-width="110px">
        <!--        <el-form-item label="课题ID" prop="ktId">-->
        <!--          <el-input v-model="LldForm.ktId" placeholder="请输入课题ID"/>-->
        <!--        </el-form-item>-->
        <el-card class="box-card" style="background-color: #e4ecf0">
          <div style="display: flex; justify-content: center;">
          <span style="font-size: 20px; color: #1890ff; font-weight: 600">
            《中国水电五局研发费用——领料单》专门用于自营项目及甲供材项目（专指分包结算款不含甲供材的情况）领用研发材料；
          </span>
          </div>
        </el-card>
        <div style="height: 60vh; overflow-y: auto;color: #00afff">
          <el-tabs tab-position="top">
            <el-tab-pane label="基础信息" :lazy="true">
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item label="领料单位" prop="lldwName" style="font-weight: 600;">
                    <el-input v-model="LldForm.lldwName" disabled placeholder="请输入领料单位名称"/>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="领用时间" prop="lyTime" style="font-weight: 600;">
                    <el-date-picker clearable
                                    v-model="LldForm.lyTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择领用时间"
                                    style="width: 100%;">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item label="课题名称" prop="ktName">
                    <el-input v-model="LldForm.ktName" disabled placeholder="请输入课题名称"/>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="课题负责人" prop="ktFzr">
                    <el-input v-model="LldForm.ktFzr" placeholder="请输入课题负责人"/>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-divider content-position="center">领料明细</el-divider>
              <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                  <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddKtLldMx">添加</el-button>
                </el-col>
                <el-col :span="1.5">
                  <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteKtLldMx">删除
                  </el-button>
                </el-col>
              </el-row>
              <el-table :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                        :data="ktLldMxList" :row-class-name="rowKtLldMxIndex"
                        @selection-change="handleKtLldMxSelectionChange" ref="ktLldMx">
                <el-table-column type="selection" width="50" align="center"/>
                <el-table-column label="序号" align="center" prop="index" width="50"/>
                <el-table-column label="物货名称" prop="whName" align="center">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.whName" placeholder="物货名称"/>
                  </template>
                </el-table-column>
                <el-table-column label="用途" prop="yt" align="center">
                  <template slot-scope="scope">
                    <el-tooltip class="item" effect="dark"
                                content="领用柴油等燃料时，“用途”列次为必填项；领用材料时，该列次为非必填项。"
                                placement="top">
                      <el-input v-model="scope.row.yt" placeholder="用途"/>
                    </el-tooltip>
                  </template>
                </el-table-column>
                <el-table-column label="规格型号" prop="ggXh" align="center">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.ggXh" placeholder="规格型号"/>
                  </template>
                </el-table-column>
                <el-table-column label="单位" prop="dw" align="center">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.dw" placeholder="单位"/>
                  </template>
                </el-table-column>
                <el-table-column label="数量" prop="sl" align="center">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.sl" placeholder="数量" @input="calculateAmount(scope.row)"/>
                  </template>
                </el-table-column>
                <el-table-column label="单价(元)" prop="dj" align="center">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.dj" placeholder="单价(元)" @input="calculateAmount(scope.row)"/>
                  </template>
                </el-table-column>
                <el-table-column label="金额(元)" prop="je" align="center">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.je" disabled placeholder="金额(元)"/>
                  </template>
                </el-table-column>
                <el-table-column label="备注" prop="bz" align="center">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.bz" placeholder="备注"/>
                  </template>
                </el-table-column>

                <div slot="append"
                     style="display: flex; justify-content: space-between; align-items: center; font-weight: bold;min-height: 3vh;background-color: #D0CECE; height: 4vh;">
                  <div style="flex-basis: 80%; text-align: center;">
                    <div style=" align-items: center;">
                      {{ '合计' }}
                    </div>
                  </div>
                  <div style="flex-basis: 20%;">
                    {{ calculateTotalAmount() + '元' }}
                  </div>
                </div>
              </el-table>
              <br>
              <el-form-item label="有关事项说明" prop="ygsxSm">
                <el-input v-model="LldForm.ygsxSm" type="textarea" placeholder="请输入内容"/>
              </el-form-item>

              <el-row :gutter="24">
                <el-col :span="8">
                  <el-form-item label="物资主管" prop="wzzg">
                    <el-input v-model="LldForm.wzzg" placeholder="请输入物资主管"/>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="发料人" prop="flr">
                    <el-input v-model="LldForm.flr" placeholder="请输入发料人"/>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="领料人" prop="llr">
                    <el-tooltip class="item" effect="dark" content="“领料人”须为研发项目小组成员。" placement="top">
                      <el-input v-model="LldForm.llr" placeholder="请输入领料人"/>
                    </el-tooltip>
                  </el-form-item>
                </el-col>
              </el-row>
              <!--            <el-form-item label="备注" prop="remark">-->
              <!--              <el-input v-model="LldForm.remark" type="textarea" placeholder="请输入内容"/>-->
              <!--            </el-form-item>-->

            </el-tab-pane>
            <el-tab-pane label="领料单附件" :lazy="true">
              <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                  <el-button size="mini" icon="el-icon-upload" type="primary" @click="LldFjShangChuan">上传</el-button>
                </el-col>
                <el-col :span="1.5">
                  <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteKtLldFj">删除
                  </el-button>
                </el-col>
              </el-row>
              <el-table :data="ktLldFjList" :row-class-name="rowKtLldFjIndex"
                        @selection-change="handleKtLldFjSelectionChange" ref="ktLldFj">
                <el-table-column type="selection" width="50" align="center"/>
                <el-table-column label="序号" align="center" prop="index" width="50"/>
                <el-table-column label="文件名称" prop="fileName" :formatter="getFileNames"/>
                <el-table-column label="文件大小" prop="fileSize"/>
                <el-table-column label="操作" align="center">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleDownload(scope.row)"
                    >下载
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
          </el-tabs>
        </div>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitLldForm">确 定</el-button>
        <el-button @click="cancelLld">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="附件上传" :visible.sync="fjShangChuan" width="25%" append-to-body>
      <el-upload
        multiple
        :action="uploadFileUrl"
        :before-upload="handleBeforeUpload"
        :file-list="fileList"
        :limit="20"
        :on-error="handleUploadError"
        :on-exceed="handleExceed"
        :on-success="handleUploadSuccess"
        :show-file-list="false"
        :headers="headers"
        class="upload-file-uploader"
        ref="fileUpload"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">
          请上传
          <template> 大小不超过 <b style="color: #f56c6c">{{ 50 }}MB</b></template>
          <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b></template>
          的文件
        </div>
      </el-upload>
    </el-dialog>

    <!-- 领料单填表说明弹窗 -->
    <el-dialog :title="title" :visible.sync="lldExplain" width="30%" append-to-body center style="color: red">
      <LldExplain></LldExplain>
    </el-dialog>

  </div>
</template>

<script>
import {
  listLld,
  getLld,
  delLld,
  addLld,
  updateLld,
  insertKtLldFjByxmsbId,
  selectKtLldFjList,
  selectKtLldMxByLldId
} from "@/api/fyhs/lld";
import {getToken} from "@/utils/auth"
import Lld from "@/views/fyhs/lld";
import {parseTime} from "@/utils/ruoyi";
import LldExplain from "@/views/fyhs/lld/lldExplain.vue";

export default {
  name: "Kt",
  props: {
    ktId: { //主表页面传过来的课题ID
      type: Number, // ktId
      required: true,
      default: 0
    },
    lldwName: { //主表页面传过来的领料单位名称
      type: String,
      required: true,
    },
    ktName: { //主表页面传过来的课题名称
      type: String,
      required: true,
    },
  },
  components: {LldExplain, Lld},
  data() {
    return {
      lldExplain: false, //领料单填表说明弹窗
      fjShangChuan: false, //附件上传弹窗
      //要修改的 项目id
      lldIdEdit: "",
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      fileList: [],
      headers: {Authorization: "Bearer " + getToken()},
      number: 0,
      uploadList: [],
      fileType: ["doc", "xls", "ppt", "txt", "pdf", "docx", "zip"],
      openAddLld: false, //新增领料单弹窗
      // 遮罩层
      loading: true,
      // 选中领料单数组
      lldIds: [],
      // 子表选中数据
      checkedKtLld: [],
      // 领料单附件子表选中数据
      checkedKtLldFj: [],
      // 领料明细子表选中数据
      checkedKtLldMx: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 领料单总条数
      LldTotal: 0,
      // 费用核算领料单表格数据
      ktLldList: [],
      // 费用核算领料单表格数据
      lldList: [],
      // 领料单附件表格数据
      ktLldFjList: [],
      // 领料明细表格数据
      ktLldMxList: [],
      // 弹出层标题
      title: "",
      // 是否显示领料单弹出层
      openAddKtLld: false,
      // 文件大小时间范围
      daterangeLyTime: [],
      // 领料单查询参数
      queryLldParams: {
        pageNum: 1,
        pageSize: 10,
        lldwName: null,
        lyTime: null,
        ktName: null,
        ktFzr: null,
        wzzg: null,
        flr: null,
        llr: null,
        ktId: null,
      },
      // 领料单表单参数
      LldForm: {},
      // 领料单表单校验
      rulesLld: {
        lldwName: [
          {required: true, message: "领料单位名称不能为空", trigger: "blur"}
        ],
        lyTime: [
          {required: true, message: "领用时间不能为空", trigger: "blur"}
        ],
        ktName: [
          {required: true, message: "课题名称不能为空", trigger: "blur"}
        ],
        ktFzr: [
          {required: true, message: "课题负责人不能为空", trigger: "blur"}
        ],
        wzzg: [
          {required: true, message: "物资主管不能为空", trigger: "blur"}
        ],
        flr: [
          {required: true, message: "发料人不能为空", trigger: "blur"}
        ],
        llr: [
          {required: true, message: "领料人不能为空", trigger: "blur"}
        ],
      },
    };
  },
  watch: {
    "ktId": {
      handler(newVal, oldVal) {
        if (newVal === 0) {
          return
        }
        this.getLldList()
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    // 计算合计金额
    calculateTotalAmount() {
      return this.ktLldMxList.reduce((total, item) => total + Number(item.je), 0);
    },
    // 计算金额
    calculateAmount(row) {
      row.je = row.sl * row.dj;
    },
    openExplain() {
      this.loading = true;
      this.title = '领料单填表说明'
      this.lldExplain = true;
      this.loading = false;
    },
    parseTime,
    // 文件下载处理
    handleDownload(row) {
      const resource = row.fileName;
      // 默认方法
      this.$download.resource(resource);
    },
    getFileNames(row) {
      // 从路径中提取文件名
      return row.fileName.split('/').pop(); // 返回文件名部分
    },
    LldFjShangChuan() { //可研性报告上传弹窗
      this.fjShangChuan = true;
    },
    // 新增领料单
    handleAddLld(row) {
      this.ktId = row.ktId
      this.openAddLld = true;
      this.title = "新增领料单";
    },
    // 查询费用核算领料单列表
    getLldList() {
      this.loading = true;
      this.queryLldParams.params = {};
      if (null != this.daterangeLyTime && '' !== this.daterangeLyTime) {
        this.queryLldParams.params["beginLyTime"] = this.daterangeLyTime[0];
        this.queryLldParams.params["endLyTime"] = this.daterangeLyTime[1];
      }
      this.queryLldParams.ktId = this.ktId
      // this.queryLldParams.lldwName = this.lldwName
      // this.queryLldParams.ktName = this.ktName
      listLld(this.queryLldParams).then(response => {
        this.lldList = response.rows;
        this.LldTotal = response.total;
        this.loading = false;
      });
    },
    // 领料单取消按钮
    cancelLld() {
      this.openAddKtLld = false;
      this.resetLld();
    },
    // 领料单表单重置
    resetLld() {
      this.LldForm = {
        lldId: null,
        ktId: this.ktId,
        lldwName: this.lldwName,
        lyTime: null,
        ktName: this.ktName,
        ktFzr: null,
        ygsxSm: null,
        wzzg: null,
        flr: null,
        llr: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.ktLldFjList = [];
      this.ktLldMxList = [];
      this.resetForm("formDialog");
    },
    // 领料单搜索按钮操作
    handleLldQuery() {
      this.queryLldParams.pageNum = 1;
      this.getLldList();
    },
    // 领料单重置按钮操作
    resetLldQuery() {
      this.daterangeLyTime = [];
      this.resetForm("queryForm");
      this.handleLldQuery();
    },
    // 领料单多选框选中数据
    handleSelectionLldChange(selection) {
      this.lldIds = selection.map(item => item.lldId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 领料单新增按钮操作
    handleLldAdd() {
      this.resetLld();
      this.openAddKtLld = true;
      this.title = "添加 中国水电五局研发费用——领料单";
    },
    // 领料单修改按钮操作
    handleLldUpdate(row) {
      this.resetLld();
      const lldId = row.lldId || this.lldIds
      this.lldIdEdit = lldId;
      getLld(lldId).then(response => {
        this.LldForm = response.data;
        this.ktLldFjList = response.data.ktLldFjList;
      });
      selectKtLldMxByLldId(lldId).then(res => {
        console.log(res.data, 'sdads')
        this.ktLldMxList = res.data.ktLldMxList;
      })
      this.openAddKtLld = true;
      this.title = "修改 中国水电五局研发费用——领料单";
    },
    // 领料单提交按钮
    submitLldForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          this.LldForm.ktLldFjList = this.ktLldFjList;
          this.LldForm.ktLldMxList = this.ktLldMxList;
          if (this.LldForm.lldId != null) {
            updateLld(this.LldForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openAddKtLld = false;
              this.getLldList();
            });
          } else {
            addLld(this.LldForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openAddKtLld = false;
              this.getLldList();
            });
          }
        }
      });
    },
    // 领料单删除按钮操作
    handleLldDelete(row) {
      const lldIds = row.lldId || this.lldIds;
      this.$modal.confirm('是否确认删除费用核算领料单编号为"' + lldIds + '"的数据项？').then(function () {
        return delLld(lldIds);
      }).then(() => {
        this.getLldList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 领料明细序号 */
    rowKtLldMxIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 领料明细添加按钮操作 */
    handleAddKtLldMx() {
      let obj = {};
      console.log(obj, ';awdad')
      obj.whName = "";
      obj.yt = "";
      obj.ggXh = "";
      obj.dw = "";
      obj.sl = "";
      obj.dj = "";
      obj.je = "";
      obj.bz = "";
      this.ktLldMxList.push(obj);
    },
    /** 领料明细删除按钮操作 */
    handleDeleteKtLldMx() {
      if (this.checkedKtLldMx.length === 0) {
        this.$modal.msgError("请先选择要删除的领料明细数据");
      } else {
        const ktLldMxList = this.ktLldMxList;
        const checkedKtLldMx = this.checkedKtLldMx;
        this.ktLldMxList = ktLldMxList.filter(function (item) {
          return checkedKtLldMx.indexOf(item.index) === -1
        });
      }
    },
    /** 复选框选中数据 */
    handleKtLldMxSelectionChange(selection) {
      this.checkedKtLldMx = selection.map(item => item.index)
    },
    // 领料单附件序号
    rowKtLldFjIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    // 领料单附件删除按钮操作
    handleDeleteKtLldFj() {
      if (this.checkedKtLldFj.length === 0) {
        this.$modal.msgError("请先选择要删除的领料单附件数据");
      } else {
        const ktLldFjList = this.ktLldFjList;
        const checkedKtLldFj = this.checkedKtLldFj;
        this.ktLldFjList = ktLldFjList.filter(function (item) {
          return checkedKtLldFj.indexOf(item.index) === -1
        });
      }
    },
    // 复选框选中数据
    handleKtLldFjSelectionChange(selection) {
      this.checkedKtLldFj = selection.map(item => item.index)
    },
    // 领料单导出按钮操作
    handleLldExport() {
      this.download('fyhs/lld/export', {
        ...this.queryLldParams
      }, `lld_${new Date().getTime()}.xlsx`)
    },
    //上传文件之前的钩子
    handleBeforeUpload(file) {
      let that = this;
      // 校检文件类型
      if (this.fileType) {
        const fileName = file.name.split('.');
        const fileExt = fileName[fileName.length - 1];
        const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
        if (!isTypeOk) {
          this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
          return false;
        }
        // 校检文件大小
        if (this.fileSize) {
          const isLt = file.size / 1024 / 1024 < this.fileSize;
          if (!isLt) {
            this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
            return false;
          }
        }
        this.$modal.loading("正在上传文件，请稍候...");
        this.number++;
        return true;
      }
    },
    // 对象转成指定字符串分隔
    listToString(list, separator) {
      let strs = "";
      separator = separator || ",";
      for (let i in list) {
        strs += list[i].url + separator;
      }
      return strs !== '' ? strs.substr(0, strs.length - 1) : '';
    },
    // 上传失败
    handleUploadError() {
      this.$modal.msgError("上传文件失败，请重试");
      this.$modal.closeLoading();
    },
    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
    },
    // 上传成功
    handleUploadSuccess(res, file) {
      let that = this;
      if (res.code === 200) {
        this.uploadList.push({name: res.fileName, url: res.fileName, size: that.formatFileSize(file.size)});
        let itemFile = {
          "lldId": that.lldIdEdit,
          "fileName": res.fileName,
          "fileSize": that.formatFileSize(file.size)
        };
        // 添加数据
        insertKtLldFjByxmsbId(itemFile).then(resFil => {
          that.getLldFjList();
        })
        this.uploadedSuccessfully();
      } else {
        this.number--;
        this.$modal.closeLoading();
        this.$modal.msgError(res.msg);
        this.$refs.fileUpload.handleRemove(file);
        this.uploadedSuccessfully();
      }
      this.fjShangChuan = false
    },
    getLldFjList() {
      let that = this;
      selectKtLldFjList(that.lldIdEdit).then(resFileList => {
        that.ktLldFjList = resFileList.data;
      })
    },
    // 显示文件大小为KB, MB, GB, TB
    formatFileSize(size) {
      if (size === 0) return '0 Bytes';
      const k = 1024;
      const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
      const i = Math.floor(Math.log(size) / Math.log(k));
      return parseFloat((size / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
    },
    // 获取文件名称
    getFileName(name) {
      // 如果是url那么取最后的名字 如果不是直接返回
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1);
      } else {
        return name;
      }
    },
    // 上传结束处理
    uploadedSuccessfully() {
      if (this.number > 0 && this.uploadList.length === this.number) {
        this.fileList = this.fileList.concat(this.uploadList);
        this.uploadList = [];
        this.number = 0;
        this.$emit("input", this.listToString(this.fileList));
        this.$modal.closeLoading();
      }
    },
  }
};
</script>
